﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace HQPad.Models
{
    /// <summary>
    /// A skill requirement.
    /// Guild requirements can have the optional values of PrimaryGuildOnly and MinRank.
    /// All guild requirements have an OR relationship.  All others can be either AND or OR 
    /// relationships.
    /// A skill requirement requires a parent skill and an optional quantity of ranks in 
    /// that skill.
    /// A minimum rank requirement has only a min rank value.
    /// Non-guild skill requirements combine as either AND's or OR's.  A character must satisfy all 
    /// the AND's and at least one OR requirement.
    /// </summary>
    public class SkillRequirement
    {
        public int SkillRequirementID { get; set; }

        public int ParentSkillID { get; set; }
        public Skill ParentSkill { get; set; }

        public int? RequiredSkillID { get; set; }
        [ForeignKey("RequiredSkillID")]
        public virtual Skill RequiredSkill { get; set; }
       
        public int? GuildID { get; set; }
        public virtual Guild Guild { get; set; }

        [Display(Name = "Minimum required rank")]
        public int? MinRank { get; set; }

        [Display(Name = "The guild level required")]
        [Range(1,3, ErrorMessage="The guild level must be between 1 and 3")]
        public int? GuildLevel { get; set; }

        public bool? PrimaryGuildOnly { get; set; }

        public int? QuantityRequired { get; set; }

        [Display(Name = "Combinational Logic")]
        [MaxLength(3, ErrorMessage = "The combination logic is limited to 3 characters")]
        [RegularExpression("AND|OR")]
        public string Combination { get; set; }

        /// <summary>Build a human readable description of the requirement.</summary>
        /// <returns>The requirement description.</returns>
        public string Description()
        {
            if (RequiredSkill != null)
            {
                string quantity = QuantityRequired.HasValue && QuantityRequired.Value > 1 ? string.Format("({0} ranks)", QuantityRequired.Value) : "";
                return string.Format("skill {0}{1}", RequiredSkill.Name, quantity);
            }
            else if (Guild != null)
            {
                string level = GuildLevel.HasValue ? string.Format("Level {0} ", GuildLevel.Value) : "";
                string primary = PrimaryGuildOnly.HasValue && PrimaryGuildOnly.Value ? "primary " : "";
                string rank = MinRank.HasValue ? string.Format(" (at rank {0} or higher)", MinRank.Value) : "";
                return string.Format("{3}{1}member of {0}{2}", Guild.Name, primary, rank, level);
            }
            else if (MinRank.HasValue)
                return string.Format("Rank {0} or higher", MinRank.Value);
            else
                return "Unknown requirement";
        }
    }
}